﻿using System;
using System.Collections.Generic;
using System.Text;

namespace UpDryTwistImageReader {

    class Configuration {

        #region Constants

        const string csUPDATE_DIVIDER = "UpdateDivider";
        const string csUPDATE_SECONDS = "UpdateSeconds";
        const string csFILE_FILTER = "FileFilter";
        const string csORIENTATION = "Orientation";
        const string csDIRECTORY_LIST = "DirectoryList";
        const string csPATH_NAME = "PathName";
        const string csRELOAD_MINUTES = "ReloadMinutes";

        #endregion

        #region Construction

        public Configuration(Rainmeter.Settings.InstanceSettings instance) {

            m_configName = instance.ConfigName;

            m_updateDivider = readInt(instance, csUPDATE_DIVIDER, 0);
            m_updateSeconds = readInt(instance, csUPDATE_SECONDS, 0);
            m_reloadMinutes = readInt(instance, csRELOAD_MINUTES, 60 * 24); // reload every 24 hours ...
            m_fileTypes     = readList(instance, csFILE_FILTER, ';' );

            if (m_fileTypes.Count == 0) {
                // If nothing specified, default to everything we can read ...
                m_fileTypes.Add(".jpg");
                m_fileTypes.Add(".jpeg");
                m_fileTypes.Add(".gif");
                m_fileTypes.Add(".png");
            }

            // If nothing specified, default to Landscape.
            string strOrientation = readString(instance, csORIENTATION, null);
            if (strOrientation != null && strOrientation.ToLower().CompareTo("vertical") == 0) {
                m_orientation = ImageOrientation.Portrait;
            }
            else {
                m_orientation = ImageOrientation.Landscape;
            }

            m_directories = readList(instance, csDIRECTORY_LIST, ';');
            string strPath = readString(instance, csPATH_NAME, null);
            if (strPath != null && !m_directories.Contains(strPath)) {
                m_directories.Add(strPath);
            }

            log("--- Directories ---");
            foreach (string directory in m_directories) {
                log("   --> " + directory);
            }
        }

        #endregion

        #region Public Methods

        public void log(string logString) {

            Rainmeter.Log(Rainmeter.LogLevel.Debug, "UPDRYTWIST[" + m_configName + "] " + logString);
        }

        #endregion

        #region Accessors

        public int updateDivider { get { return m_updateDivider; } }
        public int updateSeconds { get { return m_updateSeconds; } }
        public int reloadMinutes { get { return m_reloadMinutes; } } 
        public List<string> fileTypes { get { return m_fileTypes; } }
        public List<string> directories { get { return m_directories; } }
        public ImageOrientation orientation { get { return m_orientation; } }

        #endregion

        #region Private Methods


        private int readInt ( Rainmeter.Settings.InstanceSettings instance, string param, int def ) {

            string strValue = instance.INI_Value(param);
            int    result;
            if (strValue != null && strValue.Length > 0 && int.TryParse(strValue, out result)) {
                def = result;
            }
            return def;
        }

        private string readString(Rainmeter.Settings.InstanceSettings instance, string param, string def) {

            string strValue = instance.INI_Value(param);
            if (strValue != null && strValue.Length > 0) {
                def = strValue.Trim();
            }
            return def;
        }

        private List<string> readList(Rainmeter.Settings.InstanceSettings instance, string param, char splitter) {

            List<string> list = new List<string>();

            string strConfig = readString(instance, param, null);
            if (strConfig != null) {

                string[] values = strConfig.Split(new char[] { splitter });
                foreach ( string value in values ) {
                    list.Add( value.Trim() );
                }

            }
            return list;
        }

        #endregion 

        #region Private Members

        private string m_configName;
        private int m_updateDivider;
        private int m_updateSeconds;
        private int m_reloadMinutes;
        private List<string> m_fileTypes;
        private List<string>     m_directories;
        private ImageOrientation m_orientation;

        #endregion
    }
}
